const app = getApp();
const https = require('../../utils/https');
const nextDay = new Date((Date.now() + 60 * 60 * 24 * 1000))
const nextMonthDay = new Date((Date.now() + 60 * 60 * 24 * 1000 * 30))
const formatStartDay = `${nextDay.getFullYear()}-${ ('0' + (nextDay.getMonth()+1)).substr(-2)}-${('0' + (nextDay.getDate())).substr(-2)}`
const formatEndDay = `${nextMonthDay.getFullYear()}-${ ('0' + (nextMonthDay.getMonth()+1)).substr(-2)}-${('0' + (nextMonthDay.getDate())).substr(-2)}`
console.log(formatStartDay)
console.log(formatEndDay)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    house_structrue: [],
    //房屋类型
    house_type: [],
    // 装修程度
    decoration_degree: [],
    order_sn: '',
    house_type_list: [],
    house_degree_list: [],
    house_inspection_list: [],
    loading: false,
    disabled: false,
    building_name: '',
    create_name: '',
    owner_name: '',
    owner_tel: '',
    house_design_id: 0,
    house_type_id: 0,
    house_inspection_id: 0,
    address: '',
    house_check_name1: '',
    house_house_name1: '',
    house_check_height1: '',
    house_check_name2: '',
    house_house_name2: '',
    house_check_height2: '',
    house_check_name3: '',
    house_house_name3: '',
    house_check_height3: '',
    house_check_name4: '',
    house_house_name4: '',
    house_check_height4: '',
    inspection_name: '', // 精装或毛坯
    all_area: '',
    content: '',
    act: 'add',
    id: 0,
    // 时间选择器
    date: null,
    datePlacehoder: '选择预约时间',
    start_date: formatStartDay,
    end_date: formatEndDay,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this,
      is_login_inspector = wx.getStorageSync('is_login_inspector');
    if (!is_login_inspector || is_login_inspector != 1) {
      wx.showToast({
        title: '请先登陆验房师账号',
        icon: 'none',
        duration: 1500
      })
      wx.navigateBack({
        delta: 0,
      })
      return false;
    }
    if (options && options.order_sn != '') {
      that.setData({
        order_sn: options.order_sn
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getHouseData(); // 获取基础数据
    this.getBookOrderInfo();
    this.getHouseRegisterInfo();
  },
  /**
   * @xk 获取基础的综合数据
   */
  getHouseData: function () {
    var that = this;
    https.post('getHouseData', {}).then(res => {
      that.setData({
        house_type_list: res.house_type,
        house_type_id: res.house_type[0].id,
        house_design_list: res.house_design,
        house_design_id: res.house_design[0].id,
        house_inspection_list: res.house_inspection,
        house_inspection_id: res.house_inspection[0].id,
        now_time: res.now_time
      })
    })
  },
  /**
   * @xk 获取订单信息
   */
  getBookOrderInfo: function () {
    var that = this,
      order_sn = this.data.order_sn;
    https.post('getBookOrderInfo', {
      order_sn: order_sn
    }, false, '加载中').then(res => {
      that.setData({
        info: res,
        owner_name: res.name,
        owner_tel: res.tel,
        date: res['book_time'] || ''
      })
    })
  },
  /**
   * @xk 获取验房是注册信息
   */
  getHouseRegisterInfo: function () {
    var that = this,
      order_sn = this.data.order_sn;
    https.post('getHouseRegisterInfo', {
      order_sn: order_sn
    }, false, '加载中').then(res => {
      if(res){
        that.setData({
          building_name: res.building_name,
          create_name: res.create_name,
          owner_name: res.owner_name,
          owner_tel: res.owner_tel,
          house_design_id: res.house_design_id,
          house_type_id: res.house_type_id,
          house_inspection_id: res.house_inspection_id,
          address: res.address,
          house_check_name1: res.house_check_name1,
          house_house_name1: res.house_house_name1,
          house_check_height1: res.house_check_height1,
          house_check_name2: res.house_check_name2,
          house_house_name2: res.house_house_name2,
          house_check_height2: res.house_check_height2,
          house_check_name3: res.house_check_name3,
          house_house_name3: res.house_house_name3,
          house_check_height3: res.house_check_height3,
          house_check_name4: res.house_check_name4,
          house_house_name4: res.house_house_name4,
          house_check_height4: res.house_check_height4,
          all_area: res.all_area,
          inspection_name: res.inspection_name,
          content: res.content,
          id: res.id,
          act: 'edit'
        })
      }

    })
  },
  /**
   * @xk 原提交注册信息
   * @param {} obj
   */
  submitRegister: function (obj) {
    console.log(obj)
    return false;
    var that = this,
      building_name = obj.detail.value['building_name'],
      create_name = obj.detail.value['create_name'],
      house_design_id = obj.detail.value['house_design_id'],
      house_inspection_id = obj.detail.value['house_inspection_id'],
      house_type_id = obj.detail.value['house_type_id'],
      owner_name = obj.detail.value['owner_name'],
      owner_tel = obj.detail.value['owner_tel'],
      house_check_name1 = obj.detail.value['house_check_name1'],
      house_house_name1 = obj.detail.value['house_house_name1'],
      house_check_height1 = obj.detail.value['house_check_height1'],
      house_check_name2 = obj.detail.value['house_check_name2'],
      house_house_name2 = obj.detail.value['house_house_name2'],
      house_check_height2 = obj.detail.value['house_check_height2'],
      house_check_name3 = obj.detail.value['house_check_name3'],
      house_house_name3 = obj.detail.value['house_house_name3'],
      house_check_height3 = obj.detail.value['house_check_height3'],
      house_check_name4 = obj.detail.value['house_check_name4'],
      house_house_name4 = obj.detail.value['house_house_name4'],
      house_check_height4 = obj.detail.value['house_check_height4'],
      all_area = obj.detail.value['all_area'],
      content = obj.detail.value['content'],
      order_sn = this.data.order_sn,
      act = this.data.act,
      id = this.data.id,
      inspector_id = wx.getStorageSync('inspector_id'),
      address = obj.detail.value['address'];
    if (building_name == '' || building_name.length <= 0) {
      wx.showToast({
        title: '请填写楼盘名称',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (create_name == '' || create_name.length <= 0) {
      wx.showToast({
        title: '请填写开发商名称',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (owner_name == '' || owner_name.length <= 0) {
      wx.showToast({
        title: '请填写业主姓名',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (owner_tel == '' || owner_tel.length <= 0) {
      wx.showToast({
        title: '请填写业主姓名',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else if (!(/^1[34578]\d{9}$/.test(owner_tel))) {
      wx.showToast({
        title: '请填写正确的业主手机号',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (address == '' || address.length <= 0) {
      wx.showToast({
        title: '请填写寄送地址',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    https.post('saveHouseRegister', {
      building_name: building_name,
      owner_name: owner_name,
      create_name: create_name,
      owner_tel: owner_tel,
      house_inspection_id: house_inspection_id,
      house_design_id: house_design_id,
      house_type_id: house_type_id,
      address: address,
      all_area: all_area,
      content: content,
      act: act,
      id: id,
      order_sn: order_sn,
      inspector_id: inspector_id,
      house_check_name1: house_check_name1,
      house_house_name1: house_house_name1,
      house_check_height1: house_check_height1,
      house_check_name2: house_check_name2,
      house_house_name2: house_house_name2,
      house_check_height2: house_check_height2,
      house_check_name3: house_check_name3,
      house_house_name3: house_house_name3,
      house_check_height3: house_check_height3,
      house_check_name4: house_check_name4,
      house_house_name4: house_house_name4,
      house_check_height4: house_check_height4,
    }, true, '正在保存').then(res => {
      if (res) {
        wx.navigateBack({
          delta: 0,
        })
      }
    })




  },
  // 修改订单信息
  submit(obj) {
    console.log(obj)
    const validation = {
      address: '请填写详细地址',
      building_name: '请填写小区名称',
      date: '请选择验房时间',
      owner_name: '请填写业主姓名',
      owner_tel: '请填写业主手机号'
    }
    let validationResult = true

    const values = obj.detail.value
    for(const [key, value] of Object.entries(validation)) {
     if(!values[key]) {
       wx.showToast({
         title: value,
         icon: 'none',
         duration: 2000
       })
       validationResult = false
       break
     }
    }
    if(!validationResult) return
    const {address, building_name, date, owner_name, owner_tel} = values
    const id = this.data.info.id;
    const inspector_id = wx.getStorageSync('inspector_id');
    https.post('updateInspectorOrderInfo', {
      inspector_id,
      id,
      address: building_name,
      house_area: address,
      name: owner_name,
      tel: owner_tel,
      book_time: date
    }, true, '正在保存').then(res => {
      wx.showToast({
        title: '修改成功',
        icon: 'none',
        duration: 2000
      })
    })
  },
  // 修改时间
  bindDateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },
  // 拨打电话
  makePhoneCall() {
    wx.makePhoneCall({
      phoneNumber: this.date.owner_tel
    })
  },
  goCheck() {
    console.log('开始验房')
    wx.navigateTo({
      url: "/pages/inspection_preview/preview?order_sn=" + this.data.order_sn + "&order_id=" + this.data.info.id
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
  },
})
